package com.gdut.simple.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.data.entity.History;
import com.gdut.simple.view.adapter.base.AbsRecyclerAdapter;
import com.gdut.simple.view.adapter.base.AbsViewHolder;
import com.gdut.simple.view.adapter.base.ItemTouchHelperListener;

import java.util.Collections;
import java.util.List;

public class HistoryAdapter extends AbsRecyclerAdapter<History> implements ItemTouchHelperListener {

    private OnItemRemoveListener<History> mItemRemoveListener;

    public HistoryAdapter(Context context, List<History> list) {
        super(context, list);
    }

    @Override
    protected AbsViewHolder createHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mInflater.inflate(R.layout.layout_my_history_item, parent, false));
    }

    @Override
    protected void showViewHolder(AbsViewHolder holder, int position) {
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        viewHolder.title.setText(mData.get(position).getTitle());
        viewHolder.time.setText(mData.get(position).getCreatedAt());
    }

    @Override
    public void onItemMove(int from, int to) {
        Collections.swap(mData, from, to);
        notifyItemMoved(from, to);
    }

    @Override
    public void onItemDissmiss(int position) {
        if (mItemRemoveListener != null) {
            mItemRemoveListener.remove(mData.get(position), position);
        }
        mData.remove(position);
        notifyItemRemoved(position);
    }

    public void setOnItemRemoveListener(OnItemRemoveListener<History> listener) {
        this.mItemRemoveListener = listener;
    }

    public interface OnItemRemoveListener<T> {
        void remove(T t, int position);
    }

    private static class ItemViewHolder extends AbsViewHolder {

        TextView title;
        TextView time;

        ItemViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.id_history_title);
            time = (TextView) itemView.findViewById(R.id.id_history_time);
        }
    }

}
